package com.android.server.mock;

import android.annotation.NonNull;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterWrapper {
    private FileWriter fileWriter;

    public FileWriterWrapper(@NonNull File file, boolean createNewFile) {
        try {
            if (file.exists()) {
                if (createNewFile) {
                    file.delete();
                    file.createNewFile();
                }
            } else {
                file.createNewFile();
            }
            this.fileWriter = new FileWriter(file, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void write(String content) {
        try {
            this.fileWriter.append(content);
            this.fileWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            if (this.fileWriter != null) {
                this.fileWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
